<?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2018/11/29
 * Time: 17:39
 */

namespace App\Http\Middleware;

use Closure;

class LogdbMiddleware
{
    //用户唯一标识
    const X_UUID = 'x-uuid';

    //用户操作系统
    const X_SYSTEM = 'x-system';

    //用户手机品牌
    const X_BRAND = 'x-brand';

    //用户手机型号
    const X_MODEL = 'x-model';

    //应用程序版本
    const X_APP_VERSION = 'x-app-version';

    //微信版本
    const X_VERSION = 'x-version';

    //应用主体
    const X_APP_SUBJECT = 'x-app-subject';

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $time = self::millisecond();
        $response = $next($request);
        if (env('APP_ENV') == 'production') {
            $businessData = $request->input();
            $param = json_encode($businessData);
            $path = $request->path();   //访问的路径
            $uid = $request->header(static::X_UUID);  //用户唯一ID
            $system = $request->header(static::X_SYSTEM);     //用户操作系统
            $brand = $request->header(static::X_BRAND);       //手机品牌
            $model = $request->header(static::X_MODEL);       //手机型号
            $version = $request->header(static::X_VERSION);   //微信版本
            $appVersion = $request->header(static::X_APP_VERSION);    //应用程序版本
            $appSubject = $request->header(static::X_APP_SUBJECT);    //应用程序主体
            $data = [
                'module' => 'evaluation',
                'uid' => $uid ? $uid : 0,
                'apiName' => $path,
                'requestMethod' => $request->method(),
                'paramJson' => substr($param, 0, 1000),
                'response' => '',
                'status' => 200,
                'errCode' => 0,
                'errMessage' => '',
                'userIp' => '',
                'serverIp' => '',
                'endTime' => 0,
                'spendTime' => 0,
                'system' => $system ? $system : '',
                'brand' => $brand ? $brand : '',
                'model' => $model ? $model : '',
                'version' => $version ? $version : '',
                'appVersion' => $appVersion ? $appVersion : '',
                'appSubject' => $appSubject ? $appSubject : '',
            ];

            $endTime = self::millisecond();
            $data['response'] = substr($response->content(), 0, 600);
            $data['status'] = intval($response->getStatusCode());
            $data['errCode'] = intval(formatArrValue($response->original, 'errcode', 200));
            $data['errMessage'] = formatArrValue($response->original, 'msg', '');
            $ipArr = explode(',', getIp());
            $data['userIp'] = trim($ipArr[count($ipArr) - 1]);
            $data['serverIp'] = $_SERVER["SERVER_ADDR"] ? $_SERVER["SERVER_ADDR"] : '';
            $data['endTime'] = intval($endTime / 1000);
            $data['spendTime'] = intval($endTime - $time);

            $message = json_encode($data);
            $len = strlen($message);

            $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
            @socket_sendto($sock, $message, $len, 0, '39.106.157.197', 1217);
            socket_close($sock);
        }

        return $response;
    }

    private static function millisecond() {
        list($msec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }
}